
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netease.financial.base.lockpattern;

import android.content.Context;
import android.util.Log;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;

/**
 * Keeps the lock pattern/password data and related settings for each user.
 * Used by LockPatternUtils. Needs to be a service because Settings app also needs
 * to be able to save lockscreen information for secondary users.
 *
 * @hide
 */
public class LockSettingsService {

    private static final String TAG = "LockSettingsService";

    private final Context mContext;

    private final LockSettingsStorage mStorage;

    public LockSettingsService(Context context) {
        mContext = context;
        mStorage = new LockSettingsStorage(context);
    }

    public boolean havePattern(String userId) {
        // Do we need a permissions check here?
        return mStorage.hasPattern(userId);
    }

    public void setLockPattern(String pattern, String userId) {
        if (pattern == null) {
            mStorage.writePatternHash(null, userId);
            return;
        }

        byte[] hash = getHash(pattern, userId);
        mStorage.writePatternHash(hash, userId);

    }

    public boolean checkPattern(String pattern, String userId) {
        byte[] storedHash = mStorage.readPatternHash(userId);
        byte[] hash = getHash(pattern, userId);
        return Arrays.equals(storedHash, hash);
    }

    public void clearPattern(String userId) {
        mStorage.clearPatternHash(userId);
    }

    private byte[] getHash(String pattern, String userId) {
        String hashString = pattern + userId + getSalt(userId);
        byte[] res = hashString.getBytes();
        byte[] hash;
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            hash = md.digest(res);
        } catch (NoSuchAlgorithmException nsa) {
            hash = res;
        }
        return hash;
    }

    private String getSalt(String userId) {
        long salt = LockPatternPreferencesHelper.getSalt(userId);
        if (salt == 0) {
            try {
                salt = SecureRandom.getInstance("SHA1PRNG").nextLong();
                LockPatternPreferencesHelper.setSalt(salt, userId);
                Log.v(TAG, "Initialized lock password salt for user: " + userId);
            } catch (NoSuchAlgorithmException e) {
                // Throw an exception rather than storing a password we'll never be able to recover
                throw new IllegalStateException("Couldn't get SecureRandom number", e);
            }
        }
        return Long.toHexString(salt);
    }

}
